package com.example.maxboxtwo.components.function.Compass;

import android.Manifest;
import android.annotation.SuppressLint;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.maxboxtwo.components.activities.Compass;
import com.example.maxboxtwo.utils.ExecuteInBack;
import com.example.maxboxtwo.utils.PermissionUtils;
import com.example.maxboxtwo.utils.listeners.interfaces.ExecuteInBackListener;

import java.util.List;

public class GetCurrentPosition extends Compass implements PermissionUtils.PermissionCallbacks {
    private static final int REQUEST_PERMISSION_CODE=12;
    private LocationManager locationManager;
    private AppCompatActivity context;
    private TextView information;

    private String[] permission = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public GetCurrentPosition(LocationManager locationManager,AppCompatActivity context,TextView information) {
        this.locationManager = locationManager;
        this.context = context;
        this.information = information;
        applyAuthority();
    }

    //apply for authority about gps
    public void applyAuthority(){
        if(PermissionUtils.hasPermissions(context,permission)){
            getPosition();
        }else{
            PermissionUtils.requestPermissions(context,REQUEST_PERMISSION_CODE,permission);
        }
    }

    // starting get information of current location
    @SuppressLint("MissingPermission")
    public void getPosition(){
            boolean providerEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if(providerEnable){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10*10000,0,locationListener);
            }else{
                Toast.makeText(context,"open the gps please",Toast.LENGTH_SHORT).show();
            }
    }

    //define a listener to update content of textView
    private ExecuteInBackListener listener = new ExecuteInBackListener(){

        @Override
        public void dialog(String content) {Toast.makeText(context,content,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void showAddress(Address address) {
           String content = address.getAddressLine(0);
           Toast.makeText(context,"dfsdfsdfsdf",Toast.LENGTH_LONG).show();
           content.substring(content.indexOf("\""),content.indexOf("\""));
           content+= information.getText();
           information.setText(content);
        }
    };

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
                    information.setText(";"+"latitude:"+location.getLatitude()+";"+"longitude"+location.getLongitude());
                    // async get location from service
            /**
             * here ,has a bug no resolve
             *      new ExecuteInBack(listener,context).execute(location);
             */
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i("xyh","status changed is :");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.i("xyh","providerEnabled is :");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i("xyh","providerDisabled is :");
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

    @Override
    public void onPermissionsAllGranted(int requestCode, List<String> perms, boolean isAllGranted) {
        if(isAllGranted){
            getPosition();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    if(PermissionUtils.somePermissionPermanentlyDenied(context,perms)){
        PermissionUtils.showDialogGoToAppSettting(context);
    }else{
        PermissionUtils.showPermissionReason(requestCode,context,permission,"the authority of location is essential");
    }
    }
}
